package com.smarttest.portal.refresh.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {

	private static final ObjectMapper mapper = new ObjectMapper();
	
	public static <T> T fromString(String str, Class<T> clazz) {
		try {
			return mapper.readValue(str, clazz);
		} catch (Exception e) {
			return null;
		} 
	}
	
	public static <T> Map<String, T> mapFromString(String str, Class<T> clazz) {
		try {
			return mapper.readValue(str, new TypeReference<HashMap<String, T>>() {});
		} catch (Exception e) {
			return null;
		}
	}
	
	public static <T> List<T> listFromString(String str, Class<T> clazz) {
		try {
			return mapper.readValue(str, new TypeReference<ArrayList<T>>() {});
		} catch (Exception e) {
			return null;
		}
	}
	
}
